package com.example.imageandvideo.adapter

import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.RelativeLayout
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.example.imageandvideo.R
import com.example.imageandvideo.bean.NineImageBean

/**
 *
 * @Package: com.example.imageandvideo.adapter
 * @ClassName: NineImageAdapter
 * @Author: szj
 * @CreateDate: 6/24/21 2:17 PM
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
class NineImageAdapter(data: MutableList<NineImageBean>?) :
    BaseMultiItemQuickAdapter<NineImageBean, BaseViewHolder>(data) {

    //图片
    val IMAGE = 1

    //选择
    val SELECT = 2

    init {
        addItemType(IMAGE, R.layout.nine_item_one_layout)
        addItemType(SELECT, R.layout.nine_item_two_layout)
    }

    override fun convert(helper: BaseViewHolder, item: NineImageBean) {
        when (helper.itemViewType) {
            IMAGE -> {
                val image = helper.itemView.findViewById<ImageView>(R.id.image)

                image.visibility = View.INVISIBLE
                Log.i("szjImage", item.url)

                //图片
                Glide.with(mContext)
                    .load(item.url)
                    .into(image)

                //删除按钮
                helper.setOnClickListener(R.id.image_delete) {
                    mOnClick.onDeleteClick(helper.adapterPosition - 1)
                }
            }

            SELECT -> {
                //选择图片
                setVisibility(data.size != 10, helper.itemView)
                helper.itemView.setOnClickListener {
                    mOnClick.onItemClick(data.size)
                }
            }
        }
    }

    //防止隐藏item出现空白
    //isVisible true 显示 false 不显示
    //itemView 传入的是根布局
    private fun setVisibility(isVisible: Boolean, itemView: View) {
        val param = RecyclerView.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
        )
        if (isVisible) {
            param.height = RelativeLayout.LayoutParams.WRAP_CONTENT// 这里注意使用自己布局的根布局类型
            param.width = RelativeLayout.LayoutParams.MATCH_PARENT// 这里注意使用自己布局的根布局类型
            itemView.visibility = View.VISIBLE
        } else {
            param.height = 0
            param.width = 0
            itemView.visibility = View.GONE
        }
        itemView.layoutParams = param
    }


    //名字太难想了 随便叫了
    interface OnClick {
        //最后选择图片点击事件
        fun onItemClick(count: Int)

        //删除按钮点击事件
        fun onDeleteClick(count: Int)
    }

    lateinit var mOnClick: OnClick
}